import * as serviceApi from '@/services/mbl';
import moment from 'moment';
import { routerRedux } from 'dva/router';

// 默认查询条件
const defaultCond = {
  isLock: 0,
  year: 0,
  month: 0,
  lockDateStart: null,
  lockDateEnd: null,
  pageNumber: 1,
  pageSize: 20,
};

export default {
  namespace: 'mbl',
  state: {
    data: [],
    qc: defaultCond,
    current: undefined,
  },

  effects: {
    *getMBLView({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.query, payload);
      if (callback) callback(res);
      yield put({
        type: 'save',
        payload: res,
      });
      yield put({
        type: 'saveQc',
        payload: payload,
      });
    },
    *reSetQC({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.query, defaultCond);
      yield put({
        type: 'save',
        payload: res,
      });
      yield put({
        type: 'saveQc',
        payload: defaultCond,
      });
      if (callback) callback(res);
    },
    *getBillByKey({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.getBillByKey, payload);
      if (res) {
        yield put({
          type: 'setCurrent',
          payload: res,
        });
        if (callback) callback(res);
      }
    },
    *isHasHBL({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.isHasHBL, payload);
      if (callback) callback(res);
    },
    *createOrUpdate({ payload, callback }, { call, put }) {
      const res = yield call(
        payload.id && payload.id > 0 ? serviceApi.update : serviceApi.create,
        payload
      );
      if (callback) callback(res);
    },
    *updateHeaderInfo({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.updateHeaderInfo, payload);
      yield put({
        type: 'reloadCurrent',
      })
      if (callback) callback(res);
    },
    *getRelationersByBillId({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.getRelationersByBillId, payload);
      if (callback) callback(res);
    },
    *reloadCurrent({ payload, callback }, { call, put, select }) {
      const current = yield select(state => state.mbl.current);
      if (current && current.objectNumber) {
        yield put({
          type: 'getBillByKey',
          payload: current.objectNumber,
        });
      }
    },
    *lockBill({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.lockBill, payload);
      yield put({
        type: 'reloadCurrent',
      })
      if (callback) callback(res);
    },
    *deleteMBL({ payload, callback }, { call, put, select }) {
      const qc = yield select(state => state.mbl.qc);
      const res = yield call(serviceApi.remove, payload)
      yield put({
        type: 'getMBLView',
        payload: qc,
      });
      if (callback)
        callback(res);
    },
    *updateMBL({ payload, callback }, { call, put, select }) {
      const res = yield call(serviceApi.updateMBL, payload)
      if (res) {
        yield put({
          type: 'reloadCurrent',
        })
        if (callback)
          callback(res);
      }
    },

  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveQc(state, action) {
      return {
        ...state,
        qc: action.payload,
      };
    },
    deleteItem(state, action) {
      const d = state.data;
      return {
        ...state,
        data: {
          ...d,
          dataList: d.dataList.filter(b => b.id !== action.payload)
        }
      };
    },
    setCurrent(state, action) {
      console.log("SET CURRENT:", action.payload);
      return {
        ...state,
        current: action.payload,
      };
    }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      return history.listen(({ pathname, search }) => {
        //
        const path_toTest = pathname;

        const prefix = '/Bill/';
        if (path_toTest.indexOf(prefix) == 0) {
          console.log("BILL");
        }

        const gPrefix = '/';
        if (path_toTest.indexOf(gPrefix) == 0) {
          console.log("ROOT");
        }


        if (path_toTest.indexOf(prefix) >= 0) {
          var regex = new RegExp(`${prefix}[a-zA-Z0-9]+?/`, ["g"]);
          let value = regex.exec(path_toTest);
          if (value) {
            const billNumber = value[0].replace(prefix, '').replace('/', '');
            dispatch({
              type: 'getBillByKey',
              payload: billNumber
            })
          } else {
            console.log("NO VALUE", value);
          }
        }


      });
    },
  },
};
